package com.ygsoft.tianlu_mall.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ygsoft.tianlu_mall.pojo.ActDetail;
import com.ygsoft.tianlu_mall.pojo.Message;
import com.ygsoft.tianlu_mall.pojo.Msg;
import com.ygsoft.tianlu_mall.pojo.Order;
import com.ygsoft.tianlu_mall.service.ActDetailService;
import com.ygsoft.tianlu_mall.service.MessageService;
import com.ygsoft.tianlu_mall.service.OrderService;
import com.ygsoft.tianlu_mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("personal")
public class PersonalController {
    @Autowired
    ActDetailService actDetailService;
    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;
    @Autowired
    MessageService messageService;
    //查询所有订单信息
    @RequestMapping(value = "/OrderByUserId",method = RequestMethod.GET)
    @ResponseBody
    public Msg getOrderByUserId(
            @RequestParam(value = "order_userid") int order_userid, @RequestParam(value = "pn",defaultValue = "1")Integer pn,
            @RequestParam Integer pageSize) {
        PageHelper.startPage(pn,pageSize);
        List<Order> orders = orderService.getOrderByUserId(order_userid);
        PageInfo page = new PageInfo(orders,5);
        return  Msg.succes().add("pageInfo",page);
    }
    //查询已完成的订单
    @RequestMapping(value = "/OrderDone",method = RequestMethod.GET)
    @ResponseBody
    public Msg getOrderDone(
            @RequestParam(value = "order_userid") int order_userid, @RequestParam(value = "pn",defaultValue = "1")Integer pn,
            @RequestParam Integer pageSize){
        PageHelper.startPage(pn,pageSize);
        List<Order> orders = orderService.getOrderDone(order_userid);
        PageInfo page = new PageInfo(orders,5);
        return  Msg.succes().add("pageInfo",page);
    }
    //查询未完成的订单
    @RequestMapping(value = "/OrderNotDone",method = RequestMethod.GET)
    @ResponseBody
    public Msg getOrderNotDone(
            @RequestParam(value = "order_userid") int order_userid, @RequestParam(value = "pn",defaultValue = "1")Integer pn,
            @RequestParam Integer pageSize){
        PageHelper.startPage(pn,pageSize);
        List<Order> orders = orderService.getOrderNotDone(order_userid);
        PageInfo page = new PageInfo(orders,5);
        return  Msg.succes().add("pageInfo",page);
    }

    //查询已完成订单的数量
    @RequestMapping(value = "/OrderCount", method = RequestMethod.GET)
    @ResponseBody
    public Msg getOrderCount(
            @RequestParam(value = "order_userid") int order_userid) {
        try {
            int DoneCount = orderService.getOrderDoneCount(order_userid);
            int UnDoneCount = orderService.getOrderUnDoneCount(order_userid);
            Map<String, Object> result = new HashMap<>();
            result.put("DoneCount",DoneCount);
            result.put("UnDoneCount",UnDoneCount);
            return Msg.succes().add("OrderCountResult",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }



    //查询订单的具体信息
    @RequestMapping(value = "/OrderDetail",method = RequestMethod.GET)
    @ResponseBody
    public Msg getOrderDetail(
            @RequestParam(value = "order_id") int order_id){
        List<Order> orders = orderService.getOrderDetail(order_id);
        return  Msg.succes().add("List",orders);
    }
    //个人充值
    @RequestMapping(value ="/recharge",method = RequestMethod.POST)
    @ResponseBody
    public Msg doRecharge(
            @RequestParam(value = "user_id") int user_id,@RequestParam(value = "recharge") BigDecimal recharge){
        userService.doRecharge(user_id,recharge);
        //插入一条数据进入明细表里
        actDetailService.addDoRecharge(user_id,recharge);
        //往信息表里插入成功充值的数据
        messageService.insertActMessage(user_id);
        return Msg.succes();
    }
//    //充值成功
//    @RequestMapping(value = "/addRecharge",method = RequestMethod.POST)
//    @ResponseBody
//    public Msg insertActMessage(@RequestBody Message message){
//        SimpleDateFormat sdf = new SimpleDateFormat();
//        sdf.applyPattern("yyyy-MM-DD HH:mm:ss");
//        Date date = new Date();
//        message.setMessage_createTime(date);
//        messageService.insertActMessage(message);
//        return Msg.succes();
//    }


    //个人提现
    @RequestMapping(value = "/cash",method = RequestMethod.POST)
    @ResponseBody
    public Msg doCash(
            @RequestParam(value = "user_id") int user_id,@RequestParam(value = "cash") BigDecimal cash){
        userService.doCash(user_id,cash);
        //插入一条数据进入明细表里
        actDetailService.doCash(user_id,cash);
        //往信息表里插入提现成功的提醒
        messageService.addActMessage(user_id);
        return Msg.succes();
    }
//    //提现成功
//    @RequestMapping(value = "/addCash",method = RequestMethod.POST)
//    @ResponseBody
//    public Msg addActMessage(@RequestBody Message message){
//        SimpleDateFormat sdf = new SimpleDateFormat();
//        sdf.applyPattern("yyyy-MM-DD HH:mm:ss");
//        Date date = new Date();
//        message.setMessage_createTime(date);
//        messageService.addActMessage(message);
//        return Msg.succes();
//    }
    //活动信息显示
    @RequestMapping(value = "/actMessages" ,method = RequestMethod.GET)
    @ResponseBody
    public Msg getActMessage(@RequestParam(value = "message_userid") int message_userid ){
        List<Message> messages = messageService.getActMessage(message_userid);
        return Msg.succes().add("List",messages);
    }

    //查询个人明细记录
    @RequestMapping(value = "/getDetail" ,method = RequestMethod.GET)
    @ResponseBody
    public Msg getDetail(@RequestParam(value = "act_userid")int act_userid, @RequestParam(value = "pn",defaultValue = "1")Integer pn,
                         @RequestParam Integer pageSize){
        PageHelper.startPage(pn,pageSize);
        List<ActDetail> actDetail = actDetailService.getDetail(act_userid);
        PageInfo page = new PageInfo(actDetail,5);
        return  Msg.succes().add("pageInfo",page);
    }
    //工作区的显示数据
    @RequestMapping(value = "/workshow",method = RequestMethod.GET)
    @ResponseBody
    public Msg getWorkShow(@RequestParam(value = "order_userid")int order_userid){
        List<Order> orderList= orderService.getWorkShow(order_userid);
        return Msg.succes().add("Order",orderList);
    }



}
